1 package nmp.file_utils; 2 3 import java.io.*; 4 import java.util.*; 5 6 public class DumpTree 7 { 8 public static void main(String[] args) { 9 doit( args, System.out, System.err); 10 } 11 public static int doit(String[] args, PrintStream ps, PrintStream es) { 12 if( args.length < 2) { 13 es.println( "Use: DumpTree filter ( +dir | -dir)*\n+dir - directory to include\n-dir - directory not to include\n--dir - exclude all directories with this name"); 14 return -1; 15 } 16 String basedir = System.getProperty( "user.dir"); 17 FilenameFilter filter = new SimpleFilenameFilter( args[0]); 18 Vector excludepath = new Vector(); 19 Vector excludedirname = new Vector(); 20 for( int i = 1; i < args.length; i++) { 21 if( args[i].startsWith( "--")) { 22 es.println( "Excluding name: " + args[i].substring( 2)); 23 excludedirname.addElement( args[i].substring( 2)); 24 } else if( args[i].startsWith( "-")) { 25 es.println( "Excluding path: " + args[i].substring( 1)); 26 excludepath.addElement( args[i].substring( 1)); 27 } 28 } 29 for( int i = 1; i < args.length; i++) { 30 if( args[i].startsWith( "-")) 31 continue; 32 File f = null; 33 if( args[i].startsWith( "+")) 34 f = new File( args[i].substring( 1)); 35 else 36 f = new File( args[i]); 37 dumpDir( ps, filter, f.getParentFile(), f, true, excludepath, excludedirname); 38 } 39 return 0; 40 } 41 42 /*** 43 * Dumps the contents of the given directory according to the given filter 44 * @param filter Files to be displayed 45 * @param dir File containing the directory to be dumped 46 * @param recursively True if it should dump files recursively 47 * @param excludepath Vector with paths to exclude (subdirectories are also excluded) 48 * @param excludedir Vector with directory names to exclude, independently of the path (subdirectories are also excluded) 49 */ 50 private static void dumpDir( PrintStream ps, FilenameFilter filter, File parent, File file, boolean recursively, Vector excludepath, Vector excludedirname) { 51 if( ! file.exists()) 52 return; 53 if( parent != null) { 54 Enumeration enum = excludepath.elements(); 55 while( enum.hasMoreElements()) { 56 String prefix = (String)enum.nextElement(); 57 if( parent.getPath().startsWith( prefix)) 58 return; 59 } 60 enum = excludedirname.elements(); 61 while( enum.hasMoreElements()) { 62 String prefix = (String)enum.nextElement(); 63 if( parent.getName().equalsIgnoreCase( prefix)) 64 return; 65 } 66 } 67 if( file.isDirectory() && recursively) { 68 File[] files = file.listFiles(); 69 if( files != null) 70 for( int i = 0; i < files.length; i++) 71 dumpDir( ps, filter, file, files[i], recursively, excludepath, excludedirname); 72 return; 73 } 74 if( filter.accept( parent, file.getName())) 75 ps.println( file.getPath()); 76 } 77 }

This page was automatically generated by Maven